package lanxing.future.base.chapter10;

/**
 * @author <a href="mailto:lanxing@chances.com.cn">lanxing</a>
 * @version 2019年8月4日
 *
 */

interface Service {
    void method1();

    void method2();
}

interface ServiceFactory {
    Service getService();
}

class Implementation1 implements Service {

    public Implementation1() {
    }

    @Override
    public void method1() {
        System.out.println("imp1 : method1");

    }

    @Override
    public void method2() {
        System.out.println("imp1 : method2");
    }

    public static ServiceFactory factory = new ServiceFactory() {

        @Override
        public Service getService() {
            return new Implementation1();
        }
    };
}

class Implementation2 implements Service {

    public Implementation2() {
    }

    @Override
    public void method1() {
        System.out.println("imp2 : method1");

    }

    @Override
    public void method2() {
        System.out.println("imp2 : method2");
    }

    public static ServiceFactory factory = new ServiceFactory() {

        @Override
        public Service getService() {
            return new Implementation2();
        }
    };
}

public class Factories {
    public static void serviceConsumer(ServiceFactory factory) {
        Service service = factory.getService();
        service.method1();
        service.method2();
    }
    
    public static void main(String[] args) {
        serviceConsumer(Implementation1.factory);
        serviceConsumer(Implementation2.factory);
    }
}
